package com.test.client.customTimeLine;

import java.util.ArrayList;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.netthreads.gwt.simile.timeline.client.Theme;
import com.netthreads.gwt.simile.timeline.client.TimelineXMLHandler;

public class TimeLineWidget extends Widget {
	private EventSource eventSource = null;
	private Theme theme = null;
	private ArrayList<BandInfo> bandInfos = null;
	private ArrayList bandHotZones = null;
	private ArrayList bandDecorators = null;
	private CustomTimeLine timeLine = null;
	private Element divElement = null;
	private ITimeLineRender renderer = null;
	private final Duration visibleDuration = null;

	public TimeLineWidget(String height, String width, ITimeLineRender render) {
		bandInfos = new ArrayList();
		bandHotZones = new ArrayList();
		bandDecorators = new ArrayList();
		theme = createTheme();
		theme.setEventLabelWidth(400);
		eventSource = EventSource.create();
		divElement = DOM.createDiv();

		divElement.setId("simileTimelineBaseDivElement");
		divElement.setPropertyInt("Width", 600);
		divElement.setPropertyInt("height", 300);

		setElement(divElement);
		setHeight(height);
		setWidth(width);
		this.renderer = render;
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		if (timeLine == null)
			initialise();
	}

	public void initialise() {
		create();
		layout();
	}

	public void create() {
		renderer.render(this);
		timeLine = CustomTimeLine.create(bandInfos, eventSource, divElement, getClientElement());
	}

	public Theme createTheme() {
		Theme theme = Theme.create();
		return theme;
	}

	public void layout() {
		if (visible() && getTimeLine() != null) {
			getTimeLine().layout();
		}

	}

	public void clearBubbles() {
		if (visible()) {
			int count = bandInfos.size();

			while (--count > 0) {
				timeLine.closeBubble(count);
			}
		}

	}

	public void clearData() {
		eventSource.clear();
	}

	/**
	 * Load data into widget through handler.
	 * 
	 * @param dataUrl
	 * @param handler
	 */
	public void load(String dataUrl, TimelineXMLHandler handler) {
		timeLine.loadXML(dataUrl, handler);

	}

	/**
	 * Load data into widget through EventSource object.
	 * 
	 * @param dataUrl
	 */
	public void load(String dataUrl) {
		eventSource.loadXML(dataUrl);
	}

	/**
	 * Is timeline visible within containing view
	 * 
	 * @return visible status
	 */
	public boolean visible() {
		/**
		 * There might be an issue around this to do with how many views the widget is embedded down
		 * into. This will examine the visibility of the client div and the parent but if you were
		 * to put a parent view inside yet another view and stick that inside a tab, you might get
		 * into trouble.
		 */
		Element clientElement = getClientElement();
		Element containerElement = getElement();

		boolean client = UIObject.isVisible(clientElement);
		boolean container = UIObject.isVisible(containerElement);

		return (client && container);
	}

	/**
	 * Get client div element reference.
	 * 
	 * @return element ref
	 */
	private Element getClientElement() {
		Element element = getElement();
		if (getParent() != this && getParent() != null)
			element = getParent().getElement();

		return (element);
	}

	public CustomTimeLine getTimeLine() {
		return timeLine;
	}

	public EventSource getEventSource() {
		return eventSource;
	}

	public ArrayList getBandDecorators() {
		return bandDecorators;
	}

	public ArrayList getBandHotZones() {
		return bandHotZones;
	}

	public ArrayList<BandInfo> getBandInfos() {
		return bandInfos;
	}

	public Theme getTheme() {
		return theme;
	}

}
